package com.jwthhealth_pub.old.diet.view.fragment.adatper;

import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.constitution.ConstitutionTypeUtil;
import com.jwthhealth_pub.old.diet.presenter.IDietPresneterComple;

/**
 * Created by Administrator on 2016/12/27.
 */
public class DietConstutionAdatper extends RecyclerView.Adapter {

    private Context mContext;
    IDietPresneterComple iDietpresenter;
    private Handler mHander;
    private ProgressBar progressBar;
    private boolean loading;

    public void setLoading(boolean loading) {
        this.loading = loading;
    }

    public void setProgressBar(ProgressBar progressBar) {
        this.progressBar = progressBar;
    }

    public DietConstutionAdatper(Context context) {
        this.mContext = context;
        iDietpresenter = new IDietPresneterComple(context);
    }

    public void setmHander(Handler mHander) {
        this.mHander = mHander;
        iDietpresenter.setHandler(mHander);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_idet_constituation, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;

        final String typeLetter = ConstitutionTypeUtil.getTypeLetter(position);
        String typeName = ConstitutionTypeUtil.getTypeName(typeLetter);
        Resources resources = mContext.getResources();
        switch (position) {
            case 0:
                viewHolder.title.setText(typeName);
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_one_one));
                break;
            case 1:
                viewHolder.title.setText(typeName);
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_one_two));
                break;
            case 2:
                viewHolder.title.setText(typeName);
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_one_thi));
                break;
            case 3:
                viewHolder.title.setText(typeName);
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_one_for));
                break;
            case 4:
                viewHolder.title.setText(typeName);
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_one_fiv));
                break;
            case 5:
                viewHolder.title.setText(typeName);
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_one_six));
                break;
            case 6:
                viewHolder.title.setText(typeName);
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_one_sev));
                break;
            case 7:
                viewHolder.title.setText(typeName);
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_one_eig));
                break;
            case 8:
                viewHolder.title.setText(typeName);
                viewHolder.btn.setBackground(resources.getDrawable(R.mipmap.diet_one_nin));
                break;
            default:break;
        }


        viewHolder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (loading){
                    return;
                }

                switch (typeLetter){
                    case "a":
                        iDietpresenter.requestSecInfo("1", typeLetter);
                        break;
                    case "b":
                        iDietpresenter.requestSecInfo("1", typeLetter);
                        break;
                    case "c":
                        iDietpresenter.requestSecInfo("1", typeLetter);
                        break;
                    case "d":
                        iDietpresenter.requestSecInfo("1", typeLetter);
                        break;
                    case "e":
                        iDietpresenter.requestSecInfo("1", typeLetter);
                        break;
                    case "f":
                        iDietpresenter.requestSecInfo("1", typeLetter);
                        break;
                    case "g":
                        iDietpresenter.requestSecInfo("1", typeLetter);
                        break;
                    case "h":
                        iDietpresenter.requestSecInfo("1", typeLetter);
                        break;
                    case "i":
                        iDietpresenter.requestSecInfo("1", typeLetter);
                        break;
                    default:break;
                }
                progressBar.setVisibility(View.VISIBLE);
                loading = true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return 9;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        
        TextView title;
        RelativeLayout btn;

        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.item_title);
            btn = (RelativeLayout) itemView.findViewById(R.id.btn_click);
        }
    }
}
